package com.huluobo.day11_im.addFriend.presenter;

import android.content.Context;

import com.huluobo.day11_im.IMApp;
import com.huluobo.day11_im.R;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

public class AddFriendPresenter implements AddFriendContract.Presenter {
    private AddFriendContract.View view;
    private Context context;

    public AddFriendPresenter(AddFriendContract.View view, Context context) {
        this.view = view;
        this.context = context;
    }

    @Override
    public void add(String username, String reason) {
        if (username.matches(IMApp.REGEX_USERNAME)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().contactManager().addContact(username, reason);
                        view.onAddSuccess();
                    } catch (HyphenateException e) {
                        view.onAddFailed(e.getMessage());
                        throw new RuntimeException(e);
                    }
                }
            }).start();

        } else {
            view.onAddFailed(context.getResources().getString(R.string.login_username_text));
        }
    }
}
